#include "loadingoverlay.h"

LoadingOverlay::LoadingOverlay(QWidget* parent) : QWidget(parent), angle(0)
{
    setAttribute(Qt::WA_NoSystemBackground);
    // setAttribute(Qt::WA_TransparentForMouseEvents);
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    setWindowModality(Qt::WindowModal);

    // 初始化定时器
    timer.setInterval(40); // 50ms刷新一次
    connect(&timer, &QTimer::timeout, this, [this](){
        angle = (angle + 30) % 360; // 每次旋转30度
        update();
    });
}

void LoadingOverlay::showOverlay()
{
    timer.start();
    resize(parentWidget()->size());
    move(0, 0);
    show();
}

void LoadingOverlay::hideOverlay()
{
    if(timer.isActive())
    {
        timer.stop();
        hide();
    }
}

void LoadingOverlay::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制半透明背景
    painter.setBrush(QColor(0, 0, 0, 150));
    painter.drawRect(rect());

    // 绘制旋转圆圈
    painter.translate(width()/2, height()/2);
    painter.rotate(angle);

    QPen pen(Qt::white);
    pen.setWidth(4);
    painter.setPen(pen);
    painter.drawArc(QRectF(-30, -30, 60, 60), 0, 270*16); // 3/4圆弧
}
